/*
https://leetcode-cn.com/problems/random-flip-matrix/solution/sui-ji-fan-zhuan-ju-zhen-by-leetcode-sol-pfmr/
 */
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Solution519 {

    Map<Integer,Integer> map;
    int total;
    int n,m;

    public Solution519(int m, int n) {
        map=new HashMap<>();
        total=0;
        this.n=n;
        this.m=m;
    }

    public int[] flip() {
        int t=(int)(Math.random()*(n*m-total));
        total++;
        int p=map.getOrDefault(t,t);
        map.put(t,map.getOrDefault(n*m-total,n*m-total));
        return new int[]{p/n,p%n};
    }

    public void reset() {
        map.clear();
        total=0;
    }

    public static void main(String[] args) {
        Solution519 solution519=new Solution519(3,1);
        System.out.println(Arrays.toString(solution519.flip()));
        System.out.println(Arrays.toString(solution519.flip()));
        System.out.println(Arrays.toString(solution519.flip()));
        solution519.reset();
        System.out.println(Arrays.toString(solution519.flip()));
    }
}
